<template src="./template.html">

</template>

<script>
import util from "../../utils/util";
import ubt from '../../utils/ubt.js'
import  system from "../../utils/system.js"
import configMes from '../../utils/configMes.js'
import {previewService} from '@/services'
import { Toast } from "mint-ui";
import footTemplate from '../footTemplate.vue';
import _ from 'lodash'
import queryString from 'query-string'
const parsed = queryString.parse(location.search);
const code = parsed.code || null;

    export default {
        data() {
            var serverUrl="http://skyforest.static.elab-plus.com";
            return {
                serverUrl:serverUrl,
                templateContentId:'',
                phone:'',//手机号
                //title:'投放宝',
                customerId:'',//在线顾问跳转变量
                isSend:'', //是否跟顾问聊过天
                verifyText:'获取验证码',//验证码文案
                templateId:'',//模板ID
                projectId:'',//项目ID
                //imgUrl:'',//空白模板图片
                templateType:'',//模板类型
                verify:'',//验证码
                houseid:'',
                channel:'',//渠道
                sharerImage:'',//二维码
                sendingCode:false,//控制验证码
                submitFlag:false,//防止多次提交
                appcodeImg:'',//底部二维码
                uid:'',//uid
                source:4,//来源
                platform:4,//渠道来源
                sessionId:'',//sessionId
                //id:'',//投放内容ID
                onlineFlag:'',//在线状态控制
                tmpprojectId:'',
                trueProjectId:'',
                showLogo:true,//控制logo显示
                keyvalue:'tfb.laypoint.page.enter',//埋点 key值
                telephone:'',//底部电话号码
                appQrCode:'',//底部二维码
                address:'',
                owner:'',
            }
        },
        props:{
            content:{
                default:null,

            },
        },
        created(){
            this.templateContentId = this.$route.query.templateContentId;

            if(this.projectId){//绑定小程序
                this.source = '3';
            }
            if(this.templateContentId){//未绑定小程序
                this.source = '4';
            }

            if(this.sendingCode){
                this.getVerifyFn();
            }

        },
        mounted() {
            // if(this.content.logoUrl.includes('template10')){
            //     this.showLogo=false;
            // }
            try{
             window.getWidth= function(){
                if(window.innerWidth!= undefined){
                    return window.innerWidth;
                }
                else{
                    var B= document.body, D= document.documentElement;
                    return B==null?D.clientWidth:Math.min(D.clientWidth, B.clientWidth);
                }
            }
            console.log("******",window.getWidth(),innerWidth)
            let mywidth= window.getWidth() > 640?"640":window.getWidth();
            document.documentElement.style.fontSize = 100 * mywidth / 750 + 'px';
            }
            catch(e){
                alert(e.message);
            }
            this.sharerImage = this.$route.query.sharerImage||'';
            this.channel = this.$route.query.fromChannel||'';
            this.templateContentId = this.$route.query.templateContentId;
            this.trueProjectId = this.$route.query.projectId||'';
            this.uid=configMes.getUuid();
            if(sessionStorage.getItem('sid')){
                this.sessionId=sessionStorage.getItem('sid');
            }else{
              sessionStorage.setItem('sid',new Date().getTime());
                this.sessionId=sessionStorage.getItem('sid');
            }

            if(localStorage.getItem('uid')){
                this.uid=localStorage.getItem('uid');
            }else{
              localStorage.setItem('uid', configMes.getUuid());
                this.uid=localStorage.getItem('uid');
            }
            console.log(this.houseId,'jjj')
            this.customerId = localStorage.getItem('localCustomerId'+this.$store.state.tfbContent1.houseId);
            this.isSend = localStorage.getItem(this.customerId+'&isSendMsg'+this.$store.state.tfbContent1.houseId);
            if(this.templateContentId){//未绑定小程序
                //this.source = 4;
            }
            if(system.isIosApp()){
                this.source = 1;
            }
            if(system.isAndroidApp()){
                this.source = 2;
            }

            this.telephone = this.$store.state.tfbContent1.telephone;
            this.appQrCode = this.$store.state.tfbContent1.appQrCode;
            this.projectId = this.$store.state.tfbContent1.projectId;
            this.address = this.$store.state.tfbContent1.address;
            this.owner = this.$store.state.tfbContent1.owner;
            this.tfbContentId = this.$store.state.tfbContent1.id;
            console.log('仓库',this.telephone,this.appQrCode,this.$store.state.tfbContent1)

        },
        updated(){

        },
        beforeDestroy(){
            document.documentElement.style.fontSize ='16px';
        },
        methods: {
            async verifyCodeFn(){//验证码
                let data = {
                    sms_mobile: this.phone,
                    sms_template: 'verification.code',
                    sms_code: this.$store.state.tfbContent1.houseId==109 ? 'SMS_004' : 'SMS_002',
                    sms_type: '1',
                    sms_params: '{"":""}'
                };
                console.log('---验证码---',data)
                let result = await previewService.verifyCode(data);
                if(result.data.success){
                }else{
                    Toast(result.data.message)
                }
            },
            async checkVerifyCodeEffective(){// 校验验证码
                if(this.submitFlag){
                    return;
                }
                let data = {
                    mobile: this.phone,
                    type: 'verification.code',
                    verifycode: this.verify
                };
                console.log('---校验验证码---',data)
                let result = await previewService.checkVerifyCodeEffective(data);
                if(result.data.success){
                    this.submitFlag = true;
                    this.telData();
                    this.layPointFn();
                    Toast('提交成功')
                }else{
                    this.submitFlag = false;
                    Toast(result.data.message)
                }
            },
            async telData(){//留电
                let data = {
                    houseId: this.$store.state.tfbContent1.houseId,
                    customerMobile: this.phone,
                    sharerImage: this.sharerImage,
                    channel:this.channel,
                    platForm:this.platform,
                    refer:location.href
                };

                let result = await previewService.telData(data);
                if(result.data.success){
                    Toast('提交成功')
                }else{
                    Toast(result.data.message);
                }
            },
            async layPointFn(val){//进入页面埋点

                let data = {
                    projectId:this.$store.state.tfbContent1.projectId,
                    system:this.source,
                    tfbContentId:this.$store.state.tfbContent1.id,
                    uid:this.uid,
                    version:"1.0",
                    sessionId:this.sessionId,
                    source:this.source,
                    mobile:this.phone,
                    keyvalue:this.keyvalue
                }
                let result = await previewService.layPoint(data);
                console.log(result,'dasdsadasdsad')
            },

            getVerifyFn() {
                let vm = this;
                if (this.sendingCode) {
                    return;
                }

                if (!this.phone) {
                    Toast('请输入手机号');
                    return;
                }

                if (!/^1\d{10}$/.test(this.phone)) {
                    Toast('输入的手机号不合法');
                    return;
                }
                this.sendingCode = true;
                this.verifyCodeFn();
                let time = 60;
                vm.verifyText = time + 's';
                vm.timer1 = setInterval(() => {
                    if (time > 0) {
                        time = time - 1;
                        vm.verifyText = time + 's';
                    } else {
                        vm.sendingCode = false;
                        vm.verifyText = '获取验证码';
                        clearInterval(vm.timer1);
                    }
                }, 1000)


            },
            submit(){

                let vm = this;

                if (!vm.phone) {
                    Toast('请输入手机号');
                    return;
                }
                if (!/^1\d{10}$/.test(this.phone)) {
                    Toast('输入的手机号不合法');
                    return;
                }

                if (!vm.verify) {
                    Toast('请输入验证码');
                    return;
                }
                this.keyvalue = 'tfb.laypoint.leavephone.sendverifycode.click';
                if(this.submitFlag){
                    Toast('已经提交')
                    return;
                }

                this.checkVerifyCodeEffective();

            },
            trackTel(){//电话咨询
                this.keyvalue = 'tfb.laypoint.concat.tel.click';
                this.layPointFn();
            },
            contactCounselor(){
                this.keyvalue = 'tfb.laypoint.im.click';
                this.layPointFn();
                let param = {pathName:this.$route.name,templateContentId:this.$route.query.templateContentId,projectId:this.$route.query.projectId,templateCode:this.$route.query.templateCode,houseid:this.$store.state.tfbContent1.houseId,sharerImage:this.$route.query.sharerImage,channel:this.$route.query.channel};
                console.log(param,'op')

                console.log(this.isSend,this.customerId,'ooo')
                if(this.isSend&&this.customerId){
                    this.$router.push({ name: "messageList",query:param});
                }else{
                    this.$router.push({ name: "AdviserList" ,query:param});
                }
            },
        },
        components:{
            footTemplate
        }
    }
</script>

<style lang="scss" scoped>
  @import "./style.scss"
</style>
<style>
#app{
    overflow: visible;
}
</style>
